#pragma once

#include "proto_common.h"
#include "proto_logix_plc.hpp"

#pragma pack(1)


struct CIP_Request {
	unsigned char service;
	unsigned char request_path_size;
	vector<unsigned short> path;
	vector<unsigned char> req_data;
};


struct Path_Segment {
	unsigned char path_segment_type : 3;

};

//CIP Common Specification Table 3 - 5.15.Unconnected Send Service ParametersTable 
struct Unconnected_Send_Service_Parameters {
	unsigned char priority : 4;
	unsigned char tick_time : 4;
	unsigned char time_out_ticks;
	unsigned short message_request_size;
	CIP_Request msg_req;
	unsigned char pad;
	unsigned char route_path_size;
	unsigned char reserved;
	unsigned char route_path[2];
};


class CIP_PKT : public DEV_PKT {
public:
	bool pack_unconnected_msg_send(CIP_PKT& msg,unsigned char slot) {
		DEV_PKT& req = *this;
		req.clear();
		//cip header unconnected service
		//servcie + req path size + req path  6
		string s = "52 02 20 06 24 01";
		req.pushData(s);
		s = "04 7d"; //priority tick time; 2
		req.pushData(s);
		unsigned short& message_request_size = *(unsigned short*)req.pushData(nullptr, 2);//2
		message_request_size = msg.len;

		req.pushData(msg.data, msg.len);//14

		//下面4字节
		unsigned char route_path_size = 1; //1 word;
		req.pushData(route_path_size);
		unsigned char reserved = 0;
		req.pushData(reserved);
		unsigned char objAddr[2];
		objAddr[0] = 1; //port = backplane
		objAddr[1] = slot;
		req.pushData(objAddr, 2);
		return true;
	}
	bool pack_ControlLogix_readTag(string plcTag) {
		DEV_PKT& req = *this;
		req.clear();
		unsigned char service = 0x4c;
		req.pushData(service);
		//计算path的大小
		unsigned char tagLen = plcTag.length();
		int req_path_byte_size = tagLen + 2;
		bool needPad = false;
		if (req_path_byte_size % 2 > 0) {
			req_path_byte_size++;
			needPad = true;
		}

		unsigned char req_path_size = req_path_byte_size / 2;
		req.pushData(req_path_size);
		unsigned char path_segment_type = 0x91;//ANSI Extended Symbol Segment
		req.pushData(path_segment_type);
		req.pushData(tagLen);
		req.pushData(plcTag.data(), plcTag.length());
		if (needPad)
		{
			req.pushData(nullptr, 1);
		}
		string s = "01 00";
		req.pushData(s);
		return true;
	}
};

#pragma pack()

